package assignments.sagarjoshi.springtms;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;

import java.util.Calendar;



public  class TMSDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
    public TMSDatePickerFragment() { }

        public interface OnTMSDateChangedListener
        {
            public void onTMSDateChanged(int year, int monthOfYear, int dayOfMonth);
        }
    private OnTMSDateChangedListener mListener;



    public Dialog onCreateDialog (Bundle savedInstanceState)
    {
       String[] current = ((Button)getActivity().findViewById(R.id.lbl_coursedate)).getText().toString().split("/");


        int day = Integer.parseInt(current[0]);
        int month = Integer.parseInt(current[1])-1;
        int year = Integer.parseInt(current[2]);


        return new DatePickerDialog(getActivity(), this, year, month, day);

    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            mListener = (OnTMSDateChangedListener) activity;
        } catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
    {

            mListener.onTMSDateChanged(year,monthOfYear+1,dayOfMonth);
    }




}
